package vip.xiaonuo.megvii.captures.handle;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import vip.xiaonuo.megvii.captures.handle.impl.*;
import vip.xiaonuo.modular.megviipushdata.entity.MegviiPushData;

import javax.annotation.Resource;

@Service
public class CaptureHandleCenter {

    // 人脸
    @Resource
    private FaceHandlerService faceHandlerService;

    // 人形
    @Resource
    private HumanHandlerService humanHandlerService;

    // 机动车
    @Resource
    private VehicleHandlerService vehicleHandlerService;

    // 非机动车
    @Resource
    private CycleHandlerService cycleHandlerService;

    // 车牌
    @Resource
    private PlateHandlerService plateHandlerService;

    // 人脸识别
    @Resource
    private FaceDistinguishHandlerService faceDistinguishHandlerService;

    // 报警
    @Resource
    private AlertsHandlerService alertsHandlerService;

    private CaptureHandleCenter(){}

    private ActivePushHandleImpl getStrategy(MegviiPushData data){
        // 获取所有的业务数据
        String value = data.getValue();

        JSONObject jsonBean = JSON.parseObject(value);

        if (jsonBean.containsKey("objType")){
            String objType = jsonBean.getString("objType");
            if("face".equals(objType)){
                // 细分： 是人脸识别 还是人脸抓拍 根据是否陌生人属性来判断
                if(jsonBean.containsKey("stranger_flag")){ // 人脸识别
                    return faceDistinguishHandlerService;
                }else{ // 人脸抓拍
                    return faceHandlerService;
                }
            }else if("human".equals(objType)){      // 人形
                return humanHandlerService;
            }else if("vehicle".equals(objType)){    // 机动车
                return vehicleHandlerService;
            }else if("cycle".equals(objType)){      // 非机动车
                return cycleHandlerService;
            }else if("plate".equals(objType)){      // 车牌
                return plateHandlerService;
            }
        }else if(jsonBean.containsKey("alert_events")){ // 报警数据
            return alertsHandlerService;
        }
        return null;
    }

    public void handleData(MegviiPushData data){
        ActivePushHandleImpl strategy = this.getStrategy(data);
        if(strategy==null){
            return;
        }
        strategy.handle(data);
    }

}
